package model;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
import java.util.UUID;

import android.util.Log;

public class ProductionOrder 
{
	public static final String TAG = ProductionOrder.class.getSimpleName();
	
	private UUID _dbid; // can be constructed in two ways: fromString or fromByte[]	
	private String _id;
	
	private long _duration; // duration it took to finish this order, if the order is still going the amount will be how many seconds it has been going so far.
	
	// "Date" in Java equals DateTime in c#
	
	// start and end datetime that was requested of the machine
	private Date _requestedStartDateTime;
	private Date _requestedEndDateTime;
	
	// start and end datetime that the order actually took.
	private Date _startDateTime; 
	private Date _endDateTime;
	
	// the product that was requested for this order
	private String _requestedProduct;
	private String _requestedProductDescription;
	
	// the product that was actually made
	private String _actualProduct;
	private String _actualProductDescription;
	
	// the amount of the product that was requested
	private int _requestedCount;
	// the amount of the product that has been made so far (for this order)
	private int _actualCount;
	
	
	
	public ProductionOrder(String dbid, String id, long duration, String startDateTime, String endDateTime, String requestedStartDateTime, String requestedEndDateTime, 
				String requestedProduct, String requestedProductDescription, String actualProduct, String actualProductDescription, int requestedCount, int actualCount)
	{
		_dbid = UUID.fromString(dbid);
		_id = id;
		
		_duration = duration;
		
		// the format that is expected to be in the dateTime strings order receives
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		format.setTimeZone(TimeZone.getTimeZone("Europe/Amsterdam")); // the app only works correctly in this timezone now.
		
		try {
			_startDateTime = format.parse(startDateTime);		
			_endDateTime = format.parse(endDateTime);				
			_requestedStartDateTime = format.parse(requestedStartDateTime);
			_requestedEndDateTime = format.parse(requestedEndDateTime);
		} 
		catch (ParseException e) 
		{
			Log.e(TAG, String.format("error during formatting dates: %s", e.getMessage()));
		}
		
		_requestedProduct = requestedProduct;
		_requestedProductDescription = requestedProductDescription;
		
		_actualProduct = actualProduct;
		_actualProductDescription = actualProductDescription;
		
		_actualCount = actualCount;
		_requestedCount = requestedCount;		
	}
	
	// update the variables that possibly changed since last scan
	public void processUpdates(long duration, Date endDateTime, String actualProduct, String actualProductDescription, int actualCount) 
	{
		_duration = duration;
		_endDateTime = endDateTime;
		_actualProduct = actualProduct;
		_actualProductDescription = actualProductDescription;
		_actualCount = actualCount;
	}

	// getters
	public String getDBID() {
		return _dbid.toString();
	}

	public String getID() {
		return _id;
	}
	public long getDuration() {
		return _duration;
	}

	public Date getStartDateTime() {
		return _startDateTime;
	}

	public Date getEndDateTime() {
		return _endDateTime;
	}

	public Date getRequestedStartDateTime() {
		return _requestedStartDateTime;
	}

	public Date getRequestedEndDateTime() {
		return _requestedEndDateTime;
	}

	public String getRequestedProduct() {
		return _requestedProduct;
	}

	public String getRequestedProductDescription() {
		return _requestedProductDescription;
	}

	public String getActualProduct() {
		return _actualProduct;
	}

	public String getActualProductDescription() {
		return _actualProductDescription;
	}

	public int getRequestedCount() {
		return _requestedCount;
	}

	public int getActualCount() {
		return _actualCount;
	}

	
}
